package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.SpecialMessages;
import com.isoft.result.R;
import com.isoft.service.SpecialMessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/specialMessages")
public class SpecialMessagesController {

    @Autowired
    private SpecialMessagesService specialMessagesService;

    /**
     * 分页查询特殊操作消息
     */
    @GetMapping("/page")
    public R getPage(@RequestParam(defaultValue = "1") Integer current,
                     @RequestParam(defaultValue = "10") Integer size,
                     @RequestParam(required = false) Long metaId,
                     @RequestParam(required = false) Long flightId) {
        Page<SpecialMessages> page = new Page<>(current, size);
        QueryWrapper<SpecialMessages> queryWrapper = new QueryWrapper<>();

        if (metaId != null) {
            queryWrapper.eq("meta_id", metaId);
        }
        if (flightId != null) {
            queryWrapper.eq("flight_id", flightId);
        }

        IPage<SpecialMessages> pageResult = specialMessagesService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取所有特殊操作消息
     */
    @GetMapping("/list")
    public R getList() {
        List<SpecialMessages> list = specialMessagesService.list();
        return R.ok(list);
    }

    /**
     * 根据ID获取特殊操作消息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        SpecialMessages specialMessages = specialMessagesService.getById(id);
        return R.ok(specialMessages);
    }

    /**
     * 新增特殊操作消息
     */
    @PostMapping
    public R save(@RequestBody SpecialMessages specialMessages) {
        boolean result = specialMessagesService.save(specialMessages);
        return result ? R.ok("新增成功") : R.error("新增失败");
    }

    /**
     * 更新特殊操作消息
     */
    @PutMapping
    public R update(@RequestBody SpecialMessages specialMessages) {
        boolean result = specialMessagesService.updateById(specialMessages);
        return result ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除特殊操作消息
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        boolean result = specialMessagesService.removeById(id);
        return result ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 批量删除特殊操作消息
     */
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        boolean result = specialMessagesService.removeByIds(ids);
        return result ? R.ok("批量删除成功") : R.error("批量删除失败");
    }
}